//
//  WikiDepartmentData.m
//
//  Created by lianchuang  on 14-12-5
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "WikiDepartmentData.h"


NSString *const kWikiDepartmentDataDepartmentname = @"departmentname";
NSString *const kWikiDepartmentDataDepartmentid = @"departmentid";
NSString *const kWikiDepartmentDataParentdeptid = @"parentdeptid";


@interface WikiDepartmentData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation WikiDepartmentData

@synthesize departmentname = _departmentname;
@synthesize departmentid = _departmentid;
@synthesize parentdeptid = _parentdeptid;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.departmentname = [self objectOrNilForKey:kWikiDepartmentDataDepartmentname fromDictionary:dict];
            self.departmentid = [self objectOrNilForKey:kWikiDepartmentDataDepartmentid fromDictionary:dict];
        self.parentdeptid = [self objectOrNilForKey:kWikiDepartmentDataParentdeptid fromDictionary:dict];


    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.departmentname forKey:kWikiDepartmentDataDepartmentname];
    [mutableDict setValue:self.departmentid forKey:kWikiDepartmentDataDepartmentid];
    [mutableDict setValue:self.parentdeptid forKey:kWikiDepartmentDataParentdeptid];


    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.departmentname = [aDecoder decodeObjectForKey:kWikiDepartmentDataDepartmentname];
    self.departmentid = [aDecoder decodeObjectForKey:kWikiDepartmentDataDepartmentid];
    self.parentdeptid = [aDecoder decodeObjectForKey:kWikiDepartmentDataParentdeptid];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_departmentname forKey:kWikiDepartmentDataDepartmentname];
    [aCoder encodeObject:_departmentid forKey:kWikiDepartmentDataDepartmentid];
    [aCoder encodeObject:_parentdeptid forKey:kWikiDepartmentDataParentdeptid];

}

- (id)copyWithZone:(NSZone *)zone
{
    WikiDepartmentData *copy = [[WikiDepartmentData alloc] init];
    
    if (copy) {

        copy.departmentname = [self.departmentname copyWithZone:zone];
        copy.departmentid = [self.departmentid copyWithZone:zone];
        copy.parentdeptid = [self.parentdeptid copyWithZone:zone];

    }
    
    return copy;
}


@end
